const mixin = require('../../mixins');
// const {mfutil} = getApp()
Component({
    behaviors: [mixin],
    data: {
        isFold: false, // 是否默认折叠
        speed: 0.7, // 折叠过渡时间
        panelObj: {},
    },
    properties: {
      mf_data: Object,
    },
    attached() {
        this.flodLock = true;
        this.initData();
    },
    detached() { },
    mounted(){

    },
    methods: {
        // 初始化数据
        initData() {
            const { mf_data } = this.properties;
            let obj = mf_data.children && mf_data.children.reduce((pre, cur) => {
                if (cur.className == '折叠头') {
                    pre['foldTitle'] = cur;
                    pre['foldTitleContent'] = cur.children.reduce((p, c) => {
                        if (c.isArrow) {
                            p['arrow'] = c;
                        } else if(c.isTitle){
						   p['title'] = c
						}else if(c.name1=="mc") {
							 p['mc'] = c
						}else  {
							p['style'] = JSON.parse(JSON.stringify(c.style))
							let a =  JSON.parse(JSON.stringify(c))
							a.style.marginTop = 0
                            p['children'].push(a);
                        }
                        return p;
                    }, { arrow: [], children: [] })
                } else if (cur.className == '折叠容器') {
                    pre['flodBox'] = cur
                } else if (cur.className == '折叠标题') {

				}
                return pre;
            }, {})
            this.setData({
                isFold: mf_data.collapsible,//是否默认折叠
                // speed: mf_data.speed || 0.1, //折叠过渡时间
                panelObj: obj, // 处理后的折叠面板对象
				num: (obj.foldTitle.style.width-obj.foldTitle.style.height)/(obj.foldTitle.style.width+obj.flodBox.style.height-obj.foldTitle.style.height)
            })
        },
        changePanel() {
			const { mf_data } = this.properties;
            if (!this.flodLock) return;
			this.triggerEvent('setPanel', {
				data: {
					i: mf_data.name1,
				}
			}, {
				bubbles: true,
				composed: true
			});
            this.flodLock = false;
            // mfutil.utoolsParse.processEvent(this.data.panelObj.foldTitleContent.arrow.EventList, this.data.panelObj.foldTitleContent.arrow);
            setTimeout(() => { this.flodLock = true }, 300);
            this.setData({
                isFold: !this.data.isFold
            });

			if(mf_data.children[0]?.trackingParams){
				this.triggerEvent('triggerComponent', {
					data: {
						trackData: mf_data.children[0].trackingParams,
					}
				}, {
					bubbles: true,
					composed: true
				});
			}
        },
		select(isFold,type=null) {
			const { mf_data } = this.properties;
			this.setData({
			    isFold: isFold
			});
			if(mf_data.children[0]?.trackingParams && !type){
				this.triggerEvent('triggerComponent', {
					data: {
						trackData: mf_data.children[0].trackingParams,
					}
				}, {
					bubbles: true,
					composed: true
				});
			}
		}
    },
});
